<template>
  <div style="margin: 15px">
    <div style="margin-bottom: 15px;">
      <Input style="width: 200px;" v-model="searchValue" placeholder="请输入标签名称"/>
      <Button @click="search" type="success">搜索</Button>
      <Button @click="add" type="primary" style="float: right;">添加标签</Button>
    </div>
    <Table border :columns="tagsTitle" :data="tagsList"></Table>
    <page-com :total="total" @page-change="pageChange"></page-com>
    <Modal v-model="identity" :title="modalTitle" @on-ok="submit" @on-cancel="cancel">
      <Form :model="formCon">
        <FormItem prop="标签名称">
          <Input type="text" v-model="formCon.labelName" placeholder="请输入标签名称"/>
        </FormItem>
        <FormItem prop="标签描述">
          <Input v-model="formCon.description" type="textarea" :autosize="{minRows: 2,maxRows: 3}" placeholder="请输入标签描述"/>
        </FormItem>
        <FormItem label="设置禁启">
          <i-switch size="large" v-model="formCon.status"  @on-change="statusChange">
            <span slot="open">启用</span>
            <span slot="close">禁用</span>
          </i-switch>
        </FormItem>
      </Form>
    </Modal>
  </div>
</template>

<script>
  import {formatDate} from '_com/date'
  import PageCom from '@/components/page'
  export default {
    name: 'teacher-label',
    components: {
      PageCom
    },
    data () {
      return {
        identity: false,
        modalTitle: '',
        searchValue: '',
        number: 0,
        size: 10,
        total: 0,
        formCon: {
          status: true,
          labelCategory: 1
        },
        tagsTitle: [
          {
            title: '标签名称',
            align: 'center',
            tooltip: true,
            key: 'labelName'
          },
          {
            title: '标签描述',
            align: 'center',
            tooltip: true,
            key: 'description'
          },
          {
            title: '添加人',
            align: 'center',
            tooltip: true,
            key: 'createName'
          },
          {
            title: '添加时间',
            align: 'center',
            tooltip: true,
            render: (h, params) => {
              return h('div', formatDate(new Date(params.row.createTime), 'yyyy-MM-dd hh:mm'))
            }
          },
          {
            title: '修改人',
            align: 'center',
            tooltip: true,
            key: 'updateName'
          },
          {
            title: '修改时间',
            align: 'center',
            tooltip: true,
            render: (h, params) => {
              return h('div', formatDate(new Date(params.row.updateTime), 'yyyy-MM-dd hh:mm'))
            }
          },
          {
            title: '状态',
            key: 'status',
            width: 120,
            align: 'center',
            render: (h, params) => {
              const row = params.row
              const color = row.status === '1' ? 'primary' : row.status === '0' ? 'error' : ''
              const text = row.status === '1' ? '正常' : row.status === '0' ? '禁用' : ''
              return h('Tag', {
                props: {
                  type: 'dot',
                  color: color
                }
              }, text)
            }
          },
          {
            title: '操作',
            key: 'action',
            width: 150,
            align: 'center',
            render: (h, params) => {
              return h('div', [
                h('Button', {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      this.editor()
                      let row = params.row
                      this.formCon.status = Boolean(row.status === '1' ? 1 : 0)
                      this.formCon.labelName = row.labelName
                      this.formCon.description = row.description
                      this.formCon.id = row.id
                    }
                  }
                }, '编辑'),
                h('Button', {
                  props: {
                    type: 'error',
                    size: 'small'
                  },
                  on: {
                    click: () => {
                      this.remove(params.row.id)
                    }
                  }
                }, '删除')
              ])
            }
          }
        ],
        tagsList: []
      }
    },
    mounted() {
      this.tagsPage()
    },
    methods: {
      tagsPage() {
        this.$get('/api/api/admin/label/page', {
          labelName: this.searchValue,
          number: this.number,
          size: this.size,
          labelCategory: 1
        }).then((res) => {
          if (res.code === 0) {
            this.tagsList = res.data
            this.total = res.total
          }
        })
      },
      title(title) {
        this.modalTitle = title
        this.identity = true
      },
      add() {
        this.title('添加标签')
      },
      editor() {
        this.title('编辑标签')
      },
      submit() {
        if (this.modalTitle === '添加标签') {
          this.theAdd()
        } else {
          this.theEditor()
        }
      },
      theAdd() {
        this.formCon.status = Number(this.formCon.status)
        this.$post('/api/api/admin/label/add', this.formCon).then((res) => {
          if (res.code === 0) {
            this.$Message.success('添加成功')
            this.cancel()
            this.tagsPage()
          } else {
            this.$Message.error('添加失败')
          }
        }).catch((res) => {
          this.$Message.error('添加失败')
        })
      },
      theEditor() {
        this.formCon.status = Number(this.formCon.status)
        this.$put('/api/api/admin/label/update', this.formCon).then((res) => {
          if (res.code === 0) {
            this.$Message.success('修改成功')
            this.cancel()
            this.tagsPage()
          } else {
            this.$Message.error('修改失败')
          }
        }).catch((res) => {
          this.$Message.error('修改失败')
        })
      },
      remove(id) {
        this.$deletes('/api/api/admin/label/delete', {id: id}).then((res) => {
          if (res.code === 0) {
            this.$Message.success('删除成功')
            this.tagsPage()
          } else {
            this.$Message.error('修改失败')
          }
        }).catch((res) => {
          this.$Message.error('修改失败')
        })
      },
      search() {
        this.tagsPage()
      },
      cancel() {
        this.formCon = {}
        this.formCon.status = true
        this.formCon.labelCategory = 1
      },
      pageChange(data) {
        this.number = data
        this.tagsPage()
      }
    }
  }
</script>

<style scoped>

</style>
